<%@ page import="bean.Login" %>
<%@ page import="java.util.ArrayList" %>
<%@ page import="java.sql.*" %>
<%@ page import="java.text.SimpleDateFormat" %>
<%@ page import="java.util.Date" %>
<%@ page import="bean.Hotel" %>
<%--
  Created by IntelliJ IDEA.
  User: ASUS
  Date: 2022/7/6
  Time: 16:46
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<jsp:useBean id="query" class="bean.Query" scope="session"/>
<html>
<link href="css/bootstrap.min.css" rel="stylesheet">
<head>
    <title>预订详情</title>
</head>
<body>
<%
    String TAG = "booking.jsp";
    String UserID;
    String RoomName=null;
    String roomType=null;
    try{
        Login loginBean=null;
        loginBean=(Login)session.getAttribute("loginBean");
        UserID = loginBean.getLoginID();
        if (loginBean==null){
            response.sendRedirect("login.jsp");//重定向到登录页面
            return;
        }else {
            boolean b=loginBean.getLoginID()==null || loginBean.getLoginID().length()==0;
            if (b){
                response.sendRedirect("login.jsp");//重定向到登录页面
                return;
            }else {
//                out.print("预定成功！");
                out.print("<script>alert('预订成功！')</script>");
            }
        }
    }catch (Exception exp){
        response.sendRedirect("login.jsp");//重定向到登录页面
        return;
    }

    System.out.println("用户账号" + UserID);
    if (UserID==null){
        out.print("用户账号不存在！");
        return;
    }

    //生成数据，插入数据库
    Hotel hotel=null;
    hotel= (Hotel) session.getAttribute("hotelBean");
    String hotelId= hotel.getHotelName();//获得hotelID
    String roomId= request.getParameter("RoomID");
    roomId = roomId.replace("\"","");

    String orderId=GenerateOrder();//订单号
    Date date = new Date();//下单时间
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
    String orderDate = simpleDateFormat.format(date);
    String Checkin= query.getCheckin();
    String Checkout = query.getCheckout();
    ArrayList<String> hotelID = new ArrayList<>();
    try{
        //连接数据库
        Class.forName("org.mariadb.jdbc.Driver");
        Connection conn = DriverManager.getConnection("jdbc:mariadb://localhost:3306/hotel-management-system","root","123456");
        System.out.println( TAG +"数据库连接成功！");
        PreparedStatement stat;
        ResultSet rs;

        //修改rooms表中房间预定情况
//        ArrayList<String> roomid = new ArrayList<String>();
//        ArrayList<String> roomName = new ArrayList<String>();
//        String roomId = null;
        //查询相应房间号
        String select = "select * from rooms where HotelID ='" + hotelId + "'and RoomID='" + roomId+"'";
        stat = conn.prepareStatement(select);
        rs = stat.executeQuery();
        rs.beforeFirst();
        while (rs.next()){
//            roomid.add(rs.getString("RoomID"));
//            roomName.add(rs.getString("RoomName"));
            RoomName=rs.getString("RoomName");
            roomType=rs.getString("roomType");

//            //修改房间状态
//            String update = "update rooms set RoomCondi=0 where RoomID='" + roomId +"'" ;
//            stat = conn.prepareStatement(update);
//            int result=stat.executeUpdate();
//            if (result>0){
//                System.out.println("返回成功");
////                request.getRequestDispatcher("/detail.jsp").forward(request,response);
//            }
        }

        //向orders插入订单数据
        String insert = "insert into orders(OrderID,UserID,RoomType,HotelID,OrderDate,Checkin,Checkout,OrderCondition,RoomID,RoomName) values(?,?,?,?,?,?,?,?,?,?)";
        stat = conn.prepareStatement(insert);
        stat.setString(1,orderId);
        stat.setString(2,UserID);
        stat.setString(3,roomType);
        stat.setString(4,hotelId);
        stat.setString(5,orderDate);
        stat.setString(6,Checkin);
        stat.setString(7,Checkout);
        stat.setString(8,"成功预订");
        stat.setString(9,roomId);
        stat.setString(10,RoomName);
        System.out.println("测试boking116行：" + hotelId+Checkin+Checkout);//检查是否获取到hotelid
        //检测是否插入成功
        int result=stat.executeUpdate();
        if (result>0){
            System.out.println("返回成功");
//                request.getRequestDispatcher("/UpdateServlet").forward(request,response);
        }

        conn.close();
    }catch (ClassNotFoundException | SQLException e){
        e.printStackTrace();
    }
%>
<button type="button" class="btn btn-info"><a href="index.jsp">返回首页</a></button>
</body>
</html>
<%!
    private static int count=0;
    private static final int total=999;
    private static final SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMddHHmmssSSS");

    private static String getNowDateStr(){
        return sdf.format(new Date());
    }

    private static String now= null;

    private String GenerateOrder() {
        String datastr = getNowDateStr();
        if (datastr.equals(now)) {
            count++;// 自增
        } else {
            count = 1;
            now = datastr;
        }
        int countInteger = String.valueOf(total).length() - String.valueOf(count).length();// 算补位
        String bu = null;// 补字符串
        for (int i = 0; i < countInteger; i++) {
            bu = bu + "0";
        }
        bu += String.valueOf(count);
        if (count >= total) {
            count = 0;
        }
        return datastr + bu;
    }
%>